<template>
  <el-dialog title="教师列表" :visible.sync="visible" width="1000px" append-to-body :before-close="handleClose">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true"  label-width="68px">
      <el-form-item label="班级名称" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入班级名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="教师名称" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入教师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="subjectName">
        <el-input
          v-model="queryParams.subjectName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table ref="infoTable" v-loading="loading" :data="infoList" @select="handleSelect" @select-all="handleSelectAll" stripe border height="400px">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="班级名称" align="center" prop="className" />
      <el-table-column label="教师名称" align="center" prop="teacherName" />
      <el-table-column label="课程名称" align="center" prop="subjectName" />
      <el-table-column label="排课星期" align="center" prop="weekdayList" >
        <template  v-slot:default="scope">
          <dict-tag :options="dict.type.sys_weekDay" :value="scope.row.weekdayList"/>
        </template>
      </el-table-column>
      <el-table-column label="每日开始时间" align="center" prop="startTime" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listClassroom } from '@/api/edu/classroom'

export default {
  name: 'SelectTeacherDialog',
  dicts: ['sys_weekDay'],
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: true,
      total: 0,
      infoList: [],
      selectedTeacherRow: null,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        className: null,
        teacherName: null,
        subjectName: null
      }
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        this.getList()
      }
    }
  },
  methods: {
    getList() {
      this.loading = true
      listClassroom(this.queryParams).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelect(selection, row) {
      this.$refs.infoTable.clearSelection()
      if (selection.length > 0) {
        this.$refs.infoTable.toggleRowSelection(row, true)
        this.selectedTeacherRow = row
      }
    },
    handleSelectAll(selection) {
      if (selection.length > 0) {
        this.$refs.infoTable.clearSelection()
        this.selectedTeacherRow = null
      }
    },
    submit() {
      if (!this.selectedTeacherRow) {
        this.$message.warning('请先选择一个班级')
        return
      }
      this.$emit('select', this.selectedTeacherRow)
      this.$emit('update:visible', false)
      this.reset()
    },
    cancel() {
      this.$emit('update:visible', false)
      this.reset()
    },
    reset() {
      this.selectedTeacherRow = null
      if (this.$refs.infoTable) {
        this.$refs.infoTable.clearSelection()
      }
    },
    // 关键修复：处理右上角关闭按钮
    handleClose(done) {
      this.$emit('update:visible', false)
      this.reset()
      done()
    }
  }
}
</script>

<style scoped>
.dialog-footer {
  text-align: center;
}
</style>
